﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;


namespace LabMonitorRemote
{
    public class DewkLogger
    {
        private string _LoggerName;
        private string _IpAddress;
        private string _LoggerDescription;
        private DateTime _LastDownloadedThrough;
        private Boolean _ScheduleAutoDownloads;
        private Boolean _DisplayF;
        private Int32 _DatabaseKey;
        private Int32 _RecordingInterval;
        // add array or lsit for holding last 60 records of all 4 channels

        // Channels Enabled:
        private Boolean _Ch1Enabled, _Ch2Enabled;

        // Channel 1 limits:
        private Double _Ch1TempHigh, _Ch1TempLow, _Ch1HumidHigh, _Ch1HumidLow;
        // Channel 2 limits:
        private Double _Ch2TempHigh, _Ch2TempLow, _Ch2HumidHigh, _Ch2HumidLow;
        
        public DewkLogger(string Name, string IP_Address)
        {
            this.LoggerName = Name;
            this.IpAddress = IP_Address;
        }
        public DewkLogger(string Name, string IP_Address, DateTime LastDownloadedThroughDate)
        {
            this.LoggerName = Name;
            this.IpAddress = IP_Address;
            this.LastDownloadedThrough = LastDownloadedThroughDate;
        }
        public DewkLogger(
            string Name, 
            string Description, 
            string IP_Address, 
            Boolean DispF,
            Int32 RecInterval,
            DateTime LastDownloadedThroughDate,
            
            Boolean Chan1Enabled = false,  
            Double Ch1THigh = 100, 
            Double Ch1TLow = 0, 
            Double Ch1HHigh = 100, 
            Double Ch1HLow = 0,
            
            Boolean Chan2Enabled = false,
            Double Ch2THigh = 100, 
            Double Ch2TLow = 0,
            Double Ch2HHigh = 100, 
            Double Ch2HLow = 0,
            
            Int32 key = 0)
        {
            this.LoggerName = Name;
            this.IpAddress = IP_Address;
            this.LoggerDescription = Description;
            this.DisplayF = DispF;
            this.RecordingInterval = RecInterval;
            this.LastDownloadedThrough = LastDownloadedThroughDate;
            this.DatabaseKeyValue = key;

            this.Ch1Enabled = Chan1Enabled;
            this.Ch1TempHigh = Ch1THigh;
            this.Ch1TempLow = Ch1TLow;
            this.Ch1HumidHigh = Ch1HHigh;
            this.Ch1HumidLow = Ch1HLow;

            this.Ch2Enabled = Chan2Enabled;
            this.Ch2TempHigh = Ch2THigh;
            this.Ch2TempLow = Ch2TLow;
            this.Ch2HumidHigh = Ch2HHigh;
            this.Ch2HumidLow = Ch2HLow;
        }        

        // PROPERTIES
        #region Properties
        public string LoggerName
        {
            get { return _LoggerName; }
            private set { _LoggerName = value; }
        }
        public string IpAddress
        {
            get {return _IpAddress;}
            private set 
            {
                if (LMRStaticUtilities.ValidIPAddress(value))
                {
                    _IpAddress = value;
                }
                else
                {
                    MessageBox.Show("Attempted to create DewkLogger with invalid IP Address.\n" + value + " is not a valid IPv4 address", "Invalid IP Address");
                }
            }
        }
        public Int32 DatabaseKeyValue
        {
            get { return _DatabaseKey; }
            private set
            {
                if (value > 0)
                {
                    _DatabaseKey = value;
                }
                else if (value == 0)
                {
                    _DatabaseKey = value;
                }
                else
                {
                    MessageBox.Show("Database Key Value must be greater than or equal to 0");
                }
            }
        }

        public string LoggerDescription
        {
            get { return _LoggerDescription; }
            private set { _LoggerDescription = value; }
        }
        public DateTime LastDownloadedThrough
        {
            get { return _LastDownloadedThrough; }
            set
            {
                _LastDownloadedThrough = value;
            }
        }
        public Boolean ScheduleAutomaticDownloads
        {
            get { return _ScheduleAutoDownloads; }
            set { _ScheduleAutoDownloads = value; }
        }
        public Boolean DisplayF
        {
            get { return _DisplayF; }
            private set { _DisplayF = value; }
        }
        public Int32 RecordingInterval
        {
            get { return _RecordingInterval; }
            private set { _RecordingInterval = value; }
        }

        public Boolean Ch1Enabled
        {
            get { return _Ch1Enabled; }
            private set { _Ch1Enabled = value; }
        }
        public Boolean Ch2Enabled
        {
            get { return _Ch2Enabled; }
            private set { _Ch2Enabled = value; }
        }
        public Double Ch1TempHigh
        {
            get { return _Ch1TempHigh; }
            private set { _Ch1TempHigh = value; }
        }
        public Double Ch1TempLow
        {
            get { return _Ch1TempLow; }
            private set { _Ch1TempLow = value; }
        }
        public Double Ch2TempHigh
        {
            get { return _Ch2TempHigh; }
            private set { _Ch2TempHigh = value; }
        }
        public Double Ch2TempLow
        {
            get { return _Ch2TempLow; }
            private set { _Ch2TempLow = value; }
        }
        public Double Ch1HumidHigh
        {
            get { return _Ch1HumidHigh; }
            private set {_Ch1HumidHigh = value; }
        }
        public Double Ch1HumidLow
        {
            get { return _Ch1HumidLow; }
            private set {_Ch1HumidLow = value;}
        }
        public Double Ch2HumidHigh
        {
            get { return _Ch2HumidHigh; }
            private set { _Ch2HumidHigh = value; }
        }
        public Double Ch2HumidLow
        {
            get { return _Ch2HumidLow; }
            private set { _Ch2HumidLow = value; }
        }
        #endregion Properties
        //END PROPERTIES

        
        
        // remove -- use LMRStaticUtilities.ValidIPAddress insdead
        private bool ValidIPAddress(string ip)
        {
            string[] octets = ip.Split(".".ToCharArray());
            if (octets.Length < 4)
            {
                // not a valid IPv4 string in X.X.X.X format
                return false;
            }
            else
                foreach (string octet in octets)
                {
                    byte checkPart = 0;
                    if (!byte.TryParse(octet, out checkPart))
                    {
                        // not a valid IPv4 string in X.X.X.X format
                        return false;
                    }
                }
            // string is a valid IPv4 string in X.X.X.X format
            return true;
        }
        

    }
}
